package com.guit.hrms.controller;

import com.guit.hrms.dto.otherDept.AddOtherDeptDTO;
import com.guit.hrms.po.otherDept.OtherDept;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.OtherDeptService;
import com.guit.hrms.vo.otherDept.OtherDeptVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/department/otherDept")
public class OtherDeptController {

    @Autowired
    OtherDeptService otherDeptService;

    /**
     * 新增其它部门
     * @param addOtherDeptDTO
     * @return
     */
    @PostMapping
    public Result addOtherDept(@Valid @RequestBody AddOtherDeptDTO addOtherDeptDTO) {
        otherDeptService.addOtherDept(addOtherDeptDTO);
        return Result.success();
    }

    /**
     * 查看其它部门,不传otherDeptId则查询所有其它部门
     * @param otherDeptId
     * @return
     */
    @GetMapping
    public Result queryOtherDept(Integer otherDeptId) {
        List<OtherDeptVO> otherDeptVOList = otherDeptService.queryOtherDept(otherDeptId);
        if (otherDeptVOList == null || otherDeptVOList.isEmpty()) {
            return Result.error("未找到相关部门信息");
        }
        return Result.success(otherDeptVOList);
    }

    /**
     * 更新其它部门
     * @param otherDept
     * @return
     */
    @PatchMapping
    public Result updateOtherDept(@RequestBody OtherDept otherDept) {
        if (otherDept.getOtherDeptId() == null) {
            return Result.error("其它部门id不能为空");
        }
        if (
                otherDept.getOtherDeptNumber() == null &&
                        otherDept.getOtherDeptParentId() == null &&
                        otherDept.getOtherDeptName() == null &&
                        otherDept.getOtherDeptManagerId() == null &&
                        otherDept.getOtherDeptProfile() == null &&
                        otherDept.getOtherDeptLevel() == null
        ) {
            return Result.error("无字段更新");
        }
        otherDeptService.updateOtherDept(otherDept);
        return Result.success();
    }

    /**
     * 批量删除其它部门
     * @param deptIds
     * @return
     */
    @DeleteMapping("/{deptIds}")
    public Result deleteOtherDept(@PathVariable List<Integer> deptIds) {
        if (deptIds == null){
            return Result.error("请提交需要删除的其它部门id");
        }
        otherDeptService.deleteOtherDept(deptIds);
        return Result.success();
    }

}
